// pages/my/my/index.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    carbonCurrency: 188,
    carbonCredit: 91,
    plogNum: 10,
    avatarUrl: "../../../image/flower4.png",
    achievement: "低碳达人",
    nickName: "用户1号"
  },

  /**
   * 跳转碳币
   */
  toCurrency(e) {
    wx.navigateTo({
      url: "../../my/carbonCurrency/index" + "?num=" + this.data.carbonCurrency
    })
  },

  /**
   * 跳转碳信用
   */
  toCredit(e) {
    wx.navigateTo({
      url: "../../my/carbonCredit/index" + "?num=" + this.data.carbonCredit
    })
  },

  /**
   * 跳转帖子
   */
  toPlog(e) {
    wx.navigateTo({
      url: "../../my/plog/index" + "?num=" + this.data.plogNum
    })
  },

  /**
   * 清理缓存
   */
  clearCache() {
    wx.showToast({
      title: '清理缓存成功',
      icon: 'none',
    })
    wx.clearStorage()
  },

  /**
   * 获取用户信息 
   */
  getUserInfo() {
    var that = this

    wx.request({
      url: app.globalData.url + '/user_page/',
      method: "GET",
      data: {
        id: app.globalData.user.userId
      },
      headers: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        console.log(res.data[0])

        that.setData({
          carbonCurrency: res.data[0].carbonCurrency,
          carbonCredit: res.data[0].carbonCredit,
          avatarUrl: res.data[0].avatarPath,
          nickName: res.data[0].userName,
          achievement: res.data[0].lastAchievement,
          plogNum: res.data[0].plogNum
        })
      },
      fail: function (res) {
        wx.showToast({
          title: '获取信息失败',
          icon: 'error',
          duration: 2000
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    that.getUserInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})